//
//  HSYBaseViewController+NavigationBar.m
//  HSY
//
//  Created by 陈浩 on 2017/4/1.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "BaseViewController+NavigationBar.h"

const static NSInteger kDefaultGap = 20;
const static NSInteger kNavigationBarHeight = 44;

@implementation BaseViewController (NavigationBar)

-(void) setUpNavigationArea{
    [self addLeftArea];
    [self addCenterArea];
    [self addRightArea];
}

#pragma mark set&get menthod
-(NSInteger) leftAreaW{
    return ScreenWidth() / 3 - 2 * kDefaultGap; //屏幕宽度等分三分，间隔为20
}

-(NSInteger) centerAreaW{
    return self.leftAreaW + 2 * kDefaultGap;
}

-(NSInteger) rightAreaW{
    return self.leftAreaW;
}


// 添加左区域
- (void)addLeftArea {
    self.leftArea = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.leftAreaW, kNavigationBarHeight)];
    self.leftArea.backgroundColor = [UIColor randomColor];
    self.leftArea.tag = kleftAreaTag;

    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:self.leftArea]];
    [self setUpHSYNavigationBarButton];
}


// 添加中间区域
- (void)addCenterArea {
    self.centerArea = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.centerAreaW, kNavigationBarHeight)];
    self.centerArea.backgroundColor = [UIColor randomColor];
//    self.centerArea.tag = kcenterAreaTag;

    [self.navigationItem setTitleView:self.centerArea];
    [self setUpHSYNavigationBarButton];
}

// 添加右区域
- (void)addRightArea {
    self.rightArea = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.rightAreaW, kNavigationBarHeight)];
    self.rightArea.backgroundColor = [UIColor randomColor];
    self.rightArea.tag = krightAreaTag;

    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:self.rightArea]];
    [self setUpHSYNavigationBarButton];
}

-(void) setUpHSYNavigationBarButton{
    if (self.leftArea && !self.leftAreaLBtn) {
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.backgroundColor = [UIColor randomColor];
        leftButton.tag = HSYNavActionLeftAreaLeft;
        [leftButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.leftArea addSubview:leftButton];
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.leftArea.mas_left);
            make.width.mas_equalTo(@40);
            make.centerY.mas_equalTo(self.leftArea.mas_centerY);
            make.height.mas_equalTo(@30);
        }];
        self.leftAreaLBtn = leftButton;
    }
    
    if (self.centerArea && !self.titleButton) {
        UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        titleButton.titleLabel.adjustsFontSizeToFitWidth = YES;
        titleButton.titleLabel.minimumScaleFactor = 0.8;
        titleButton.backgroundColor = [UIColor randomColor];
        [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [titleButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.centerArea addSubview:titleButton];
        [titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(-10, -10, -10, -10));
        }];
        self.titleButton = titleButton;
    }
    
    if (self.rightArea && !self.rightAreaRBtn) {
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        rightButton.tag = HSYNavActionRightAreaRight;
        rightButton.backgroundColor = [UIColor randomColor];
        [rightButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.rightArea addSubview:rightButton];
        [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.rightArea.mas_right);
            make.width.mas_equalTo(@40);
            make.centerY.mas_equalTo(self.rightArea.mas_centerY);
            make.height.mas_equalTo(@30);
        }];
        self.rightAreaRBtn = rightButton;
    }
    
    
}

-(void) addLeftAreaRightButton{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.backgroundColor = [UIColor randomColor];
    rightButton.tag = HSYNavActionLeftAreaRight;
    [rightButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.leftArea addSubview:rightButton];
    [rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.leftArea.mas_right);
        make.width.mas_equalTo(@40);
        make.centerY.mas_equalTo(self.leftArea.mas_centerY);
        make.height.mas_equalTo(@30);
    }];
    self.leftAreaRBtn = rightButton;
}

-(void) addRightAreaLeftButton{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.backgroundColor = [UIColor randomColor];
    leftButton.tag = HSYNavActionRightAreaLeft;
    [leftButton addTarget:self action:@selector(handleNavButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightArea addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.rightArea.mas_left);
        make.width.mas_equalTo(@40);
        make.centerY.mas_equalTo(self.rightArea.mas_centerY);
        make.height.mas_equalTo(@30);
    }];
    self.rightAreaLBtn = leftButton;
}


-(void) setupNavigationBar:(HSYNavgationBarType) type{
    [self setUpNavigationArea];
    [self setUpHSYNavigationBarButton];
    switch (type) {
        case HSYNavgationBarTypeCompoundLeft:
        {
            [self addLeftAreaRightButton];
        }
            break;
        case HSYNavgationBarTypeCompoundRight:
        {
            [self addRightAreaLeftButton];
        }
            break;
        case HSYNavgationBarTypeCompoundLeftAndRight:
        {
            [self addLeftAreaRightButton];
            [self addRightAreaLeftButton];
        }
            break;
        default:
            break;
    }
}

-(void) handleNavButtonAction:(UIButton *) button{
    NSLog(@"标题按钮...");
    [self handleNavigationAction:(HSYNavigationActionType)button.tag];
}

@end
